# -*- coding:utf-8 -*-
__doc__ = '待我处理'

import time

from PageClass import EcsIndexPage, EcsWaitDeal, BoeCommon
from TestCases.common._baseClass import _BaseClass
from Util import logger, ZTCCLOUDError, config


class WaitDeal(_BaseClass):

    def __init__(self, driver, isExit: bool = False):
        self.isExit = isExit
        self.__goToWaitDeal(driver)
        self._ecsWaitDealPage = EcsWaitDeal(driver)
        self._boeCommon = BoeCommon(driver)

    def __exit__(self, *args):
        if super().__exit__(*args):
            if self.isExit:
                self.exit()
        else:
            self.exit()

    def exit(self):
        self._ecsWaitDealPage.driver.quit()

    def __goToWaitDeal(self, driver):
        self._ecsIndexPage = EcsIndexPage(driver)
        self._ecsIndexPage.driver.get(config.urlDict.url.ecsHost)
        self._ecsIndexPage.click_waitDeal()

    def __findBoe(self, boeNo):
        self._ecsWaitDealPage.input_boeNo(boeNo)

        sTime = time.time()
        while True:
            eTime = time.time()
            self._ecsWaitDealPage.click_boeNoSelectButton()
            try:
                status = self._ecsWaitDealPage.selectResultIsOrNot(boeNo)
            except:
                pass
            else:
                if status:
                    break
            if eTime - sTime >= 600:
                raise TimeoutError(f"{boeNo} do not exist，待我处理菜单10分钟未找到单据")
        if not status:
            raise ZTCCLOUDError(f"待我处理菜单，{boeNo} do not exist")

    def __isOrNotSuccess(self):
        if '成功' in self._ecsWaitDealPage.getToastBoxText():
            content = '作废成功'
            logger.info(f"状态为：{content}")
            return content
        else:
            content = '作废失败'
            logger.info(f"状态为：{content}")
            return content

    def __submitSuccessOrNot(self):
        if '单据状态已变更' in self._boeCommon.get_boeMessage():
            content = '提交校验成功'
        else:
            content = '提交校验失败'
        logger.info(f'状态为：{content}')
        return content

    # 作废
    def invalidBoe(self, boeNo):
        try:
            self.__findBoe(boeNo)
            self._ecsWaitDealPage.click_invalidButton()
            self._ecsWaitDealPage.click_boeBusinessTipConfirm()
            return self.__isOrNotSuccess()
        except Exception as e:
            logger.error(f"待我处理-作废出现异常，异常信息为：{str(e)}")

    # 根据单号提交单据
    def submitBoeByBoeNo(self, boeNo):
        try:
            self.__findBoe(boeNo)
            self._ecsWaitDealPage.click_boeNoIntoDetail()
            self._boeCommon.click_boeSubmitButton()
            return self._boeCommon.getBoeSuccessMessage()
        except Exception as e:
            logger.error(f"待我处理-根据单号提交单据出现异常，异常信息为：{str(e)}")

    # 1.进入单据详情；2.作废单据；3.返回单据详情，重新提交
    def invalidBoeAndRetrySubmit(self, boeNo):
        try:
            self.__findBoe(boeNo)
            self._ecsWaitDealPage.click_boeNoIntoDetail()
            self._ecsWaitDealPage.switchWindow()
            self._ecsWaitDealPage.click_invalidButton()
            self._ecsWaitDealPage.click_boeBusinessTipConfirm()
            self._ecsWaitDealPage.switchWindow()
            self._boeCommon.click_boeSubmitButton()
            return self.__submitSuccessOrNot()
        except Exception as e:
            logger.error(f"待我处理-先作废单据再提交出现异常，异常信息为：{str(e)}")
